package com.czmh.etc.onlinepubsys.utils.mingchuang;

import org.springframework.util.StringUtils;

/**
 * 数据加密
 */
public abstract class ETCDataEncUtil {

    /**
     * 加密16号文件
     *
     * @return
     */
    public static String enc16FileData(String name, String idNo, Integer idType) {
        StringBuilder sb = new StringBuilder(120);
        try {
            sb.append("04D696003B0000");
            //==================================
            int i = 0;
            byte[] bs = name.getBytes("gb2312");
            for (byte b : bs) {
                if (b < 0) {
                    sb.append(Integer.toHexString(b).substring(6, 8));
                } else {
                    sb.append(Integer.toHexString(b));
                }
                i += 2;
                //企业公司单位名称会超过规定的长度，所以做截取操作，3～22	持卡人姓名	20	持卡人姓名，编码见GB 2312
                if (i >= 40){
                    break;
                }
            }
            for (; i < 40; i++) {
                sb.append(0);
            }
            //==================================
            i = 0;
            bs = idNo.getBytes("gb2312");
            for (byte b : bs) {
                if (b < 0) {
                    sb.append(Integer.toHexString(b).substring(6, 8));
                } else {
                    sb.append(Integer.toHexString(b));
                }
                i += 2;
            }
            for (; i < 64; i++) {
                sb.append(0);
            }
            //==================================
            if (idType != 0) {
                idType = 5;
            }
            sb.append("0").append(idType);
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 加密15号文件
     *
     * @return
     */
    public static String enc15FileData(String data, String beginDate, String endDate, String vehiclePlate,
                                       Integer userType, Integer vehicleColor, Integer VehicleType) {
        StringBuilder sb = new StringBuilder(96);
        try {
            sb.append("04D695002F");
            sb.append(data).append(beginDate).append(endDate);
            //==================================
            int i = 0;
            byte[] bsName = vehiclePlate.getBytes("gb2312");
            for (byte b : bsName) {
                if (b < 0) {
                    sb.append(Integer.toHexString(b).substring(6, 8));
                } else {
                    sb.append(Integer.toHexString(b));
                }
                i += 2;
            }
            for (; i < 24; i++) {
                sb.append("0");
            }
            //==================================
            if (userType < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(userType));
            //==================================
            if (vehicleColor < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(vehicleColor));
            //==================================
            if (VehicleType < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(VehicleType));
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String hexStringToString(String s) {
        if (!StringUtils.hasLength(s)) {
            return null;
        }
        try {
            s = s.replace("0", "");
            byte[] baKeyword = new byte[s.length() / 2];
            for (int i = 0; i < baKeyword.length; i++) {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            }
            s = new String(baKeyword, "GB2312");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
}
